package com.sqfw.framework.task;

import com.sqfw.common.utils.DateUtils;
import com.sqfw.project.wp.domain.WpInfoCall;
import com.sqfw.project.wp.mapper.WpInfoCallMapper;
import com.sqfw.project.wp.mapper.WpInfoMapper;
import com.sqfw.project.zj.mapper.ZjRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 检查工单和录音关联表中的工单号是否存在工单
 *
 * @author tangxt
 */
@Component("wpInfoCallTask")
@Slf4j
public class WpInfoCallTask {

    private final String PORT_8091 = "8091";

    @Value("${server.port}")
    private String port;


    @Autowired
    private WpInfoCallMapper wpInfoCallMapper;

    @Autowired
    private WpInfoMapper wpInfoMapper;

    @Autowired
    private ZjRecordMapper zjRecordMapper;

    /**
     *  查询一个小时前的工单和录音关联数据
     *  如果工单不存在，把关联表的wpid=null
     *  */
    public void checkWpInfoCallWpId(String begainTime, String endTime) {
        if (!PORT_8091.equals(port)) {
            return;
        }
        WpInfoCall wpInfoCall = new WpInfoCall();
        // 传递的时间
        if (StringUtils.isNotBlank(begainTime) && StringUtils.isNotBlank(endTime)) {
            wpInfoCall.setBegainTime(DateUtils.parseDate(begainTime));
            wpInfoCall.setEndTime(DateUtils.parseDate(endTime));
        } else {
            Date date = new Date();
            wpInfoCall.setBegainTime(DateUtils.parseDate(DateUtils.getHourOfDayDateStr(date, -2)));
            wpInfoCall.setEndTime(DateUtils.parseDate(DateUtils.getHourOfDayDateStr(date, -1)));
        }
        // 默认根据时间查询（前一个小时），
        List<WpInfoCall> wpInfoCallList =  wpInfoCallMapper.getWpInfoCallByCreateTime(wpInfoCall);
        if (!CollectionUtils.isEmpty(wpInfoCallList)) {
            log.info("本次查询数量：{}", wpInfoCallList.size());
            for (WpInfoCall wpInfoCall1 : wpInfoCallList) {
                if (StringUtils.isNotBlank(wpInfoCall1.getWpid())) {
                    int count = wpInfoMapper.countWpInfoByWpId(wpInfoCall1.getWpid());
                    if (count == 0) {
                        int i = wpInfoCallMapper.updateWpInfoCallByScid(wpInfoCall1.getScid());
                        int m = zjRecordMapper.updateZjRecordByWpid(wpInfoCall1.getWpid());
                        log.info("修改工单和录音关联scid：{},wpid: {}, 数量i: {}, 录音数量m: {}",wpInfoCall1.getScid(), wpInfoCall1.getWpid(), i, m);
                    }else {
                        log.info("工单存在wpid：{}", wpInfoCall1.getWpid());
                    }
                }
            }
        }else {
            log.info("本次没有查询到工单和录音关联数据");
        }
    }




}




